package com.taiyi.renaissancemall.ui.classification.goodlist;

import android.content.Intent;
import android.util.Log;

import com.taiyi.renaissancemall.Utils.ToastUtils;
import com.taiyi.renaissancemall.Utils.Utils;
import com.taiyi.renaissancemall.base.BasePresent;
import com.taiyi.renaissancemall.base.MyApp;
import com.taiyi.renaissancemall.bean.BaseResultBean;
import com.taiyi.renaissancemall.bean.FindBrandListBean;
import com.taiyi.renaissancemall.net.BaseUrl;
import com.taiyi.renaissancemall.net.HttpUtils;
import com.taiyi.renaissancemall.net.callbck.JsonCallback;
import com.taiyi.renaissancemall.ui.login.LoginActivity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Response;

public class GoodListPresent extends BasePresent<GoodListView> {

    public void getGoodList(String pagenum, String lableid, String categoryid, String classificationid, String pricesort, String salesort, String bvsort, String brandid
            , String beginprice, String endprice) {
//        String ids = "";
//        if (str_ids != null) {
//            StringBuffer stringBuffer = new StringBuffer();
//            for (int i = 0; i < str_ids.size(); i++) {
//                if (i == str_ids.size() - 1) {
//                    stringBuffer.append(str_ids.get(i));
//                } else {
//                    stringBuffer.append(str_ids.get(i));
//                    stringBuffer.append(",");
//                }
//            }
//            ids = stringBuffer.toString().trim();
//            Log.d("筛选", "所选择品牌:" + brandid + "--" + str_ids.size() + "--" + ids);
//        }

        Map<String, String> map = new HashMap<>();
        map.put("pagenum", pagenum);
        map.put("classificationid", classificationid);
        map.put("categoryid", categoryid);
        map.put("pricesort", pricesort);
        map.put("lableid", lableid);
        map.put("salesort", salesort);
        map.put("bvsort", bvsort);
        map.put("brandid", brandid);
        map.put("beginprice", beginprice);
        map.put("endprice", endprice);
        HttpUtils.postRequest(BaseUrl.FIND_COMMODITY_LIST, this, map, new JsonCallback<BaseResultBean<GoodListBean>>() {
            @Override
            public BaseResultBean<GoodListBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<GoodListBean>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
//                    ToastUtils.showLongToast(response.body().msg_cn);
                    view.getGoodListSuccess(response.body().data);
                } else if (response.body().code == -1) {
                    Utils.getSpUtils().remove("token");
                    Intent intent = new Intent(MyApp.getInstance(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApp.getInstance().startActivity(intent);
                } else {
                    view.getGoodListFailure(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<GoodListBean>> response) {
                super.onError(response);
            }
        });
    }

    public void getFindBrandList() {
        Map<String, String> map = new HashMap<>();
        HttpUtils.postRequest(BaseUrl.FIND_BRAND_LIST, this, map, new JsonCallback<BaseResultBean<FindBrandListBean>>() {
            @Override
            public BaseResultBean<FindBrandListBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<FindBrandListBean>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
//                    ToastUtils.showLongToast(response.body().msg_cn);
                    view.getFindBrandListSuccess(response.body().data);
                } else if (response.body().code == -1) {
                    Utils.getSpUtils().remove("token");
                    Intent intent = new Intent(MyApp.getInstance(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApp.getInstance().startActivity(intent);
                } else {
                    view.getFindBrandFailure(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<FindBrandListBean>> response) {
                super.onError(response);
            }
        });
    }

}
